﻿using HealthcareSystem.API.Read.Application.Command.Followup;
using HealthcareSystem.API.Read.DTO.Element;
using HealthcareSystem.API.Read.DTO.Followup;
using HealthcareSystem.Domain.BasicSettings.FormManagement;
using MediatR;
using WeekExam.PassPort.ErrorCode;
using WeekExam.PassPort.Infrastructure.Interface;

namespace HealthcareSystem.API.Read.Application.CommandHandel
{
    public class ShowFormUpCommandHandel : IRequestHandler<ShowFormUpCommand, ApiResult<List<ShowFollowupDto>>>
    {
        IBaseRepository<Followup> followupHandel;
        IBaseRepository<FormCategory> formCategoryHandel;

        public ShowFormUpCommandHandel(IBaseRepository<Followup> followupHandel, IBaseRepository<FormCategory> formCategoryHandel)
        {
            this.followupHandel = followupHandel;
            this.formCategoryHandel = formCategoryHandel;
        }
        /// <summary>
        /// 表单显示处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<List<ShowFollowupDto>>> Handle(ShowFormUpCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<ShowFollowupDto>> result = new ApiResult<List<ShowFollowupDto>>();
            result.Code = ResultCode.查询成功;
            result.Message = ResultCode.查询成功.ToString();

            var list = from a in followupHandel.GetAll()
                       from b in formCategoryHandel.GetAll()
                       where b.FormCategoryId == a.FormCategoryId
                       select new ShowFollowupDto
                       {
                           FollowupId = a.FollowupId,
                           FormCategoryId = b.FormCategoryId,
                           FollowupName = a.FollowupName,
                           DateTime = a.DateTime,
                           GetDateTime = a.GetDateTime,
                           FormCategorypeople = a.FormCategorypeople,
                           IsFollowing = a.IsFollowing,
                           FormCategoryName = b.FormCategoryName,
                       };

            // ====== 添加查询过滤条件 ======



            // 2. 按元素类别（分类）过滤
            if (request.FormCategoryId.HasValue && request.FormCategoryId.Value > 0)
            {
                list = list.Where(x => x.FormCategoryId == request.FormCategoryId.Value);
            }

            result.Data = list.ToList();


            return Task.FromResult(result);
        }
    }
}
